package com.baoer.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //TODO 设置key
    public void setKey(String key,Object value){
        this.redisTemplate.opsForValue().set(key,value);
    }
    //TODO 获取key
    public Object getKey(String key){
        return this.redisTemplate.opsForValue().get(key);
    }
    //TODO 存储过期时间
    public void setExKey(String key,Object value,Long timeout){
        this.redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }
    //TODO 验证某个key是否存在
    public boolean containsKey(String key){
        return this.redisTemplate.hasKey(key);
    }
}
